package BookSearch;

import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BookDB.BookBean;
import BookDB.h2DAO;
import Loginout.UserBean;

@WebServlet("/AdvancedSearch")
public class AdvancedSearch extends HttpServlet {
	private static final long serialVersionUID = 2L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = req.getSession();
		UserBean UB = (UserBean) session.getAttribute("ub");

		resp.setContentType("text/html;charset=UTF-8");
		req.setCharacterEncoding("UTF-8");
		PrintWriter writer = resp.getWriter();
		if (UB == null) {
			resp.sendRedirect("error.html");
		} else if (UB.vaild()) {
			try {
				String bookid = req.getParameter("bookid");
				String isbn = req.getParameter("isbn");
				String bookname = req.getParameter("bookname");
				String calid = req.getParameter("calid");
				String borrow = req.getParameter("borrowing");
				String location=req.getParameter("location");
              if (bookname==null)
              {
            	  bookname="";
              }
              else
              {
            	  bookname= new String(req.getParameter("bookname").getBytes(
  						"ISO-8859-1"), "utf-8");
              }
				List<BookBean> BBlist = null;
				BBlist = h2DAO.SelectBookList(bookid,isbn,bookname,calid,borrow,location);

				req.setAttribute("blist", BBlist);
				req.getRequestDispatcher("/SearchResult.jsp")
						.forward(req, resp);

			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				writer.print("Search error!!");
			}
		}
	}
}
